package com.icss.service.usermanage;

import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import com.icss.service.BaseService;
import com.icss.util.MD5;

/**
 * 管理员管理
 * @author shixiaolong
 * @dateJun 9, 2012
 */
public class UserManageService extends BaseService{
	
	
	/**
	 * 获取用户列表
	 * @param request
	 * @return
	 */
	public List<Map> getUserList(HttpServletRequest request){
		String page_num = req.getValue(request, "page_num");
		String userid = req.getValue(request, "userid");
		String nickname = req.getValue(request, "nickname");
		String userstate = req.getValue(request, "userstate");
		String sql="select * from tbl_user where 1 = 1 ";
		if(!"".equals(userid)){
			sql+=" and userid = '"+userid+"'";
		}
		if(!"".equals(nickname)){
			sql+=" and nickname = '"+nickname+"'";
		}
		if(!"".equals(userstate)){
			sql+=" and userstate = '"+ userstate+"'";
		}
		return db.getForList(sql, Integer.parseInt(page_num), request);
	}
	
	/**
	 * 单个用户详细信息
	 * @param request
	 * @return
	 */
	public Map getUserDetail(HttpServletRequest request){
		String userid = req.getValue(request, "userid");
		String sql="select * from tbl_user where userid = ? ";
		return db.queryForMap(sql,new Object[]{userid});
		
	}
	
	/**
	 * 新增账户保存
	 * @param request
	 * @return
	 */
	public int save(HttpServletRequest request){
		String nickname = req.getValue(request, "nickname");
		String password = MD5.MD5Encode(req.getValue(request, "password"));
		String email = req.getValue(request, "email");
		String phonenumber = req.getValue(request, "phonenumber");
		String method = req.getValue(request, "method");
		String userid = req.getValue(request, "userid");
		String sql="";
		if("edit".equals(method)){
			sql="update tbl_user set nickname=?,password=?,email=?,phonenumber=? where userid=?";
			return db.update(sql,new Object[]{nickname,password,email,phonenumber,userid});
		}else{
			sql="insert into tbl_user(userid,nickname,password,email,phonenumber,userstate) values(tbl_user_s_id.nextval,?,?,?,?,1)";
			return db.update(sql,new Object[]{nickname,password,email,phonenumber});
		}
	}
	
	public int delete(HttpServletRequest request){
		String userid = req.getValue(request, "userid");
		String sql="update tbl_user set userstate= 0 where userid = ?";
		return db.update(sql,new Object[]{userid});
		
	}
}
